﻿using System;
using System.Collections.Generic;
using System.Custom.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Gizmox.WebGUI.Forms;

namespace CoCoon.Forms
{
    public static class ColorUtils
    {
        static List<Color> GetConstants(Type enumType)
        {
            MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.Public;
            PropertyInfo[] properties = enumType.GetProperties();
            List<Color> list = new List<Color>();
            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo info = properties[i];
                if (info.PropertyType == typeof(Color))
                {
                    MethodInfo getMethod = info.GetGetMethod();
                    if ((getMethod != null) && ((getMethod.Attributes & attributes) == attributes))
                    {
                        object[] index = null;
                        list.Add((Color)info.GetValue(null, index));
                    }
                }
            }
            return list;
        }

        public static List<Color> GetWebColors(bool sortByColor)
        {
            var webColors = GetConstants(typeof(Color));
            if (sortByColor)
            {
                webColors.Sort(new StandardColorComparer());
            }
            return webColors;
        }
        class StandardColorComparer : IComparer<Color>
        {
            // Methods
            public int Compare(Color color, Color color2)
            {
                if (color.A < color2.A)
                {
                    return -1;
                }
                if (color.A > color2.A)
                {
                    return 1;
                }
                if (color.GetHue() < color2.GetHue())
                {
                    return -1;
                }
                if (color.GetHue() > color2.GetHue())
                {
                    return 1;
                }
                if (color.GetSaturation() < color2.GetSaturation())
                {
                    return -1;
                }
                if (color.GetSaturation() > color2.GetSaturation())
                {
                    return 1;
                }
                if (color.GetBrightness() < color2.GetBrightness())
                {
                    return -1;
                }
                if (color.GetBrightness() > color2.GetBrightness())
                {
                    return 1;
                }
                return 0;
            }
        }

        class SystemColorComparer : IComparer<Color>
        {
            // Methods
            public int Compare(Color color, Color color2)
            {
                return string.Compare(color.Name, color2.Name, false, CultureInfo.InvariantCulture);
            }
        }
    }

    public class WebColorComboBox : ComboBox
    {
        private List<Color> _colorList;

        public WebColorComboBox()
        {
            _colorList = ColorUtils.GetWebColors(true);
            this.DataSource = _colorList;
            
        }

        public override string GetItemText(object objItem)
        {
            if (objItem is Color)
            {
                return ((Color)objItem).Name;
            }
            else
            {
                return "White";
            }
        }

        public override Color GetItemColor(object objItem)
        {
            return (Color) objItem;
        }

        protected override bool ShouldRenderColor()
        {
            return true;
        }

        protected override void OnSelectedValueChanged(EventArgs e)
        {
            base.OnSelectedValueChanged(e);
            OnSelectedColorChanged(new EventArgs<Color>(SelectedColor));
        }

        protected Color SelectedColor
        {
            get { return SelectedItem != null ? (Color) SelectedItem : new Color(); }
        }

        public event EventHandler<EventArgs<Color>> SelectedColorChanged;

        protected virtual void OnSelectedColorChanged(EventArgs<Color> e)
        {
            var handler = SelectedColorChanged;
            if (handler != null) handler(this, e);
        }
    }
}
